package paperfly.input;

// ImagesLoader.java
// Andrew Davison, February 2004, dandrew@ratree.psu.ac.th

/* The Imagesfile and images are stored in "Images/"
 (the IMAGE_DIR constant).

 ImagesFile Formats:

 o <fnm>                     // a single image file

 n <fnm*.ext> <number>       // a series of numbered image files, whose
 // filenames use the numbers 0 - <number>-1

 s <fnm> <number>            // a strip file (fnm) containing a single row
 // of <number> images

 g <name> <fnm> [ <fnm> ]*   // a group of files with different names;
 // they are accessible via  
 // <name> and position _or_ <fnm> prefix

 and blank lines and comment lines.

 The numbered image files (n) can be accessed by the <fnm> prefix
 and <number>. 

 The strip file images can be accessed by the <fnm>
 prefix and their position inside the file (which is 
 assumed to hold a single row of images).

 The images in group files can be accessed by the 'g' <name> and the
 <fnm> prefix of the particular file, or its position in the group.


 The images are stored as BufferedImage objects, so they will be 
 manipulated as 'managed' images by the JVM (when possible).
 */

import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageLoader {
	private GraphicsConfiguration gc;

	public ImageLoader() {
		initLoader();
	}

	private void initLoader() {
		GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
	}

	/*
	 * Load the image from <fnm>, returning it as a BufferedImage which is
	 * compatible with the graphics device being used. Uses ImageIO.
	 */
	public BufferedImage loadImage(String fnm) {
		return loadImage(new File(fnm));
	}
	/*
	 * Load the image from <fnm>, returning it as a BufferedImage which is
	 * compatible with the graphics device being used. Uses ImageIO.
	 */
	public BufferedImage loadImage(File fnm) {
		try {
			BufferedImage im = ImageIO.read(fnm);
			int transparency = im.getColorModel().getTransparency();
			BufferedImage copy = gc.createCompatibleImage(im.getWidth(), im.getHeight(), transparency);
			Graphics2D g2d = copy.createGraphics();
			g2d.drawImage(im, 0, 0, null);
			g2d.dispose();
			return copy;
		} catch (IOException e) {
			System.out.println("Load Image error for " + fnm + ":\n" + e);
			return null;
		}
	}

	/*
	 * Load the image from <fnm>, returning it as a BufferedImage which is
	 * compatible with the graphics device being used. Uses ImageIO.
	 */
	public BufferedImage loadImage(String fnm, int width, int height) {
		try {
			BufferedImage im = ImageIO.read(new File(fnm));
			int transparency = im.getColorModel().getTransparency();
			BufferedImage copy = gc.createCompatibleImage(width, height, transparency);
			Graphics2D g2d = copy.createGraphics();
			g2d.drawImage(im.getScaledInstance(width, height,Image.SCALE_AREA_AVERAGING), 0, 0, null);
			g2d.dispose();
			return copy;
		} catch (IOException e) {
			System.out.println("Load Image error for " + fnm + ":\n" + e);
			return null;
		}
	}

	/**
	 * Cria uma imagem buferizada a partir de uma imagem.
	 * @param im
	 * @param width
	 * @param height
	 * @return
	 */
	public static BufferedImage makeBIM(Image im, int width, int height) {
		BufferedImage copy = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = copy.createGraphics();
		g2d.drawImage(im, 0, 0, null);
		g2d.dispose();
		return copy;
	}
	/**
	 * Cria uma imagem buferizada a partir de uma imagem.
	 * @param im
	 * @param width
	 * @param height
	 * @return
	 */
	public static BufferedImage makeBIM(Image im) {
		BufferedImage copy = new BufferedImage(im.getWidth(null), im.getHeight(null),BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = copy.createGraphics();
		g2d.drawImage(im, 0, 0, null);
		g2d.dispose();
		return copy;
	}
}
